<template>
  <div class="product-card" @click="navigateToDetail">
    <div class="product-image">
      <img :src="product.image" :alt="product.name" />
      <div class="quick-actions">
        <button class="action-btn" @click.stop="addToCart">
          <i class="fas fa-shopping-cart"></i>
        </button>
        <button class="action-btn" @click.stop="toggleFavorite">
          <i :class="['fa-heart', isFavorite ? 'fas' : 'far']"></i>
        </button>
      </div>
      <div class="product-tags">
        <span v-for="tag in product.tags" :key="tag" class="tag">{{
          tag
        }}</span>
      </div>
    </div>
    <div class="product-info">
      <h3 class="product-name">{{ product.name }}</h3>
      <p class="product-desc">{{ product.description }}</p>
      <div class="product-bottom">
        <div class="price">¥{{ product.price }}</div>
        <div class="stock" :class="{ 'low-stock': product.stock < 10 }">
          库存: {{ product.stock }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "ProductCard",
  props: {
    product: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      isFavorite: false,
    };
  },
  methods: {
    addToCart() {
      this.$store.commit("addToCart", this.product);
    },
    toggleFavorite() {
      this.isFavorite = !this.isFavorite;
    },
    navigateToDetail() {
      this.$router.push(`/product/${this.product.id}`);
    },
  },
};
</script>

<style scoped>
.product-card {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s;
  cursor: pointer;
}

.product-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.15);
}

.product-image {
  position: relative;
  height: 300px;
  overflow: hidden;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s;
}

.product-card:hover .product-image img {
  transform: scale(1.05);
}

.quick-actions {
  position: absolute;
  top: 10px;
  right: 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  opacity: 0;
  transform: translateX(20px);
  transition: all 0.3s;
}

.product-card:hover .quick-actions {
  opacity: 1;
  transform: translateX(0);
}

.action-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: white;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #333;
  transition: all 0.3s;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.action-btn:hover {
  background: #ff6b6b;
  color: white;
  transform: scale(1.1);
}

.product-tags {
  position: absolute;
  top: 10px;
  left: 10px;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.tag {
  background: #ff6b6b;
  color: white;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
}

.product-info {
  padding: 20px;
}

.product-name {
  font-size: 18px;
  color: #333;
  margin-bottom: 10px;
  height: 44px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.product-desc {
  color: #666;
  font-size: 14px;
  margin-bottom: 15px;
  height: 40px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.product-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.price {
  font-size: 20px;
  color: #ff6b6b;
  font-weight: bold;
}

.stock {
  font-size: 14px;
  color: #666;
}

.low-stock {
  color: #ff6b6b;
}

@media (max-width: 768px) {
  .product-image {
    height: 250px;
  }

  .quick-actions {
    opacity: 1;
    transform: translateX(0);
  }
}
</style> 